$(function () {
   $('#save-board-btn').click(function (event) {
       event.preventDefault();
       var self = $(this);
       var dialog = $('#myModal');
       var nameE = $('input[name="name"]');
       var descE = $('input[name="desc"]');

       var name = nameE.val();
       var desc = descE.val();
       var submitType = self.attr('data-type');
       var boardId = self.attr('data-id');

       var url='';
       if (submitType == 'update'){
           url = '/cms/uboard/' ;
       }else {
           url = '/cms/aboard/'
       }
       zlajax.post({
           'url' : url ,
           'data' : {
               'name' : name,
               'desc' : desc,
               'board_id' : boardId
           },
           'success' : function (data) {
               if (data['code'] == 200){
                   //隐藏模态框
                   $('#myModal').modal('hide');
                   //重新加载页面
                   window.location.reload()
               }else {
                   xtalert.alertInfo(data['message']);
               }
           },
           'fail' : function (error) {
               xtalert.alertNetworkError('网络错误！')
           }
       });
   }) ;
});

$(function () {
   $('.edit-board-btn').click(function (event) {
       var self = $(this);
       var dialog = $('#myModal');
       dialog.modal('show');

       var tr = self.parent().parent();
       var name = tr.attr('data-name');
       var desc = tr.attr('data-desc');

       var nameE = dialog.find('input[name="name"]');
       var descE = dialog.find('input[name="desc"]');
       var saveBtn = dialog.find('#save-board-btn');

       nameE.val(name);
       descE.val(desc);
       saveBtn.attr('data-type','update');
       saveBtn.attr('data-id',tr.attr('data-id'));
   }) ;
});

$(function () {
   $('.delete-board-btn').click(function () {
       var self = $(this);
       var tr = self.parent().parent();
       var board_id = tr.attr('data-id');
       xtalert.alertConfirm({
           'msg' : '您确定要删除这个板块吗？',
           'confirmCallback' : function () {
               zlajax.post({
                   'url' : '/cms/dboard/',
                   'data' : {
                       'board_id' : board_id
                   },
                   'success' : function (data) {
                       if (data['code'] == 200){
                           window.location.reload();
                       }else {
                           xtalert.alertInfo(data['message']);
                       }
                   },
                   'fail' : function (error) {
                       xtalert.alertNetworkError('网络错误！');
                   }
               })
           }
       })
   }) ;
});
